<template>
  <div class="app-container">
    <!-- 业务受理 -->
    <!-- <update ref="update" /> -->
    <div>
      <!-- $ref来获取dom节点  ref = "ruleForm"  该点名为ruleForm   $ref['ruleForm']找到改点-->
      <el-form
        ref="ruleForm"
        :inline="true"
        :rules="rules"
        :model="map"
        size="mini"
      >
        <el-form-item label="序列号" prop="courseName">
          <el-input v-model="map.productID" clearable />
        </el-form-item>
        <el-form-item label="维修状态" prop="state">
          <el-select
            v-model="map.maintainState"
            clearable
            placeholder="状态"
            style="width: 80px"
          >
            <el-option id="normal" value="1" label="完成" />
            <el-option id="disable" value="0" label="未完成" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否免费" prop="isfree">
          <el-select
            v-model="select2"
            clearable
            placeholder="是否免费"
            style="width: 100px"
          >
            <el-option id="free" value="免费">免费</el-option>
            <el-option id="charge" value="收费">收费</el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="上下架" prop="isputon">
          <el-select
            v-model="select3"
            clearable
            placeholder="上下架"
            style="width: 100px"
          >
            <el-option id="puton" value="上架">上架</el-option>
            <el-option id="putoff" value="下架">下架</el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="selectdata">查询</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          <el-button
            type="primary"
            icon="el-icon-circle-plus-outline"
            @click="handlecreate"
          >添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      empty-text="无数据"
    >
      <el-table-column type="index" align="center" label="序号" width="50" />
      <el-table-column label="产品序列号" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.ProductID }}
          <!-- <img :src="scope.row.image" alt=""> -->
        </template>
      </el-table-column>
      <el-table-column label="客户维修产品描述" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.CustomerDescribe }}
        </template>
      </el-table-column>
      <el-table-column label="维修人员产品描述" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.WorlDescribe }}
        </template>
      </el-table-column>
      <el-table-column label="维修状态" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.MaintainState }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前维修状态" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.NowState }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修人员工号" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.WorkID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户号" width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.CustomerID }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="上下架" width="150" align="center"> -->
      <!-- <el-switch v-model="form.delivery" /> -->
      <!-- <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isPutaway"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="下架"
            active-value="0"
            inactive-text="上架"
            inactive-value="1"
            @change="changeisPutaway(scope.row.id)"
          />
        </template> -->
      <!-- </el-table-column> -->
      <!-- <el-table-column
        align="center"
        prop="created_at"
        label="状态"
        width="150"
      > -->
      <!-- <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.display_time }}</span>
        </template> -->
      <!-- <template slot-scope="scope">
          <el-switch
            v-model="scope.row.statusId"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="禁用"
            active-value="0"
            inactive-text="正常"
            inactive-value="1"
            @change="changestatusId(scope.row.id)"
          />
        </template>
      </el-table-column> -->
      <el-table-column class-name="status-col" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row.ID)">
            修改
          </el-button>
          <!-- <el-button type="danger" size="small" @click="Coursedelete(scope.row.ID)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加按钮页面 -->
    <el-dialog
      :visible.sync="createVisible"
      width="30%"
      :before-close="
        () => {
          createVisible = false
        }
      "
      :show-close="false"
    >
      <el-form
        ref="menuForm"
        :model="menuForm"
        :rules="rules"
        label-width="6vw"
      >
        <el-form-item label="序列号" prop="productID">
          <el-input
            v-model="menuForm.productID"
            placeholder="请输入产品序列号"
          />
        </el-form-item>
        <el-form-item label="客户描述" prop="customerDescribe">
          <el-input
            v-model="menuForm.customerDescribe"
            placeholder="请输入产品的问题"
          />
        </el-form-item>
        <!-- <el-form-item label="是否收费" prop="courseisfree">
          <el-radio-group v-model="menuForm.courseisfree">
            <el-radio :label="1">免费</el-radio>
            <el-radio :label="0">付费</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row v-if="menuForm.courseisfree == 0">
          <el-form-item label="课程原价" prop="courseOriginal">
            <el-input
              v-model="menuForm.courseOriginal"
              type="number"
              placeholder="请输入价格"
            />
          </el-form-item>
          <el-form-item label="优惠价格" prop="courseDiscount">
            <el-input
              v-model="menuForm.courseDiscount"
              type="number"
              placeholder="请输入优惠价格"
            />
          </el-form-item>
        </el-row> -->
        <!-- <el-form-item label="可选题库" prop="coursetiku">
          <el-input
            v-model="menuForm.coursetiku"
            placeholder="请输入题库名称关键词"
          />
        </el-form-item>
        <el-form-item label="课程简介" prop="courseintroduction">
          <el-input
            v-model="menuForm.courseintroduction"
            type="textarea"
            placeholder="输入内容不超过500字"
          />
        </el-form-item> -->
        <!-- <el-form-item label="课程封面" prop="coursecover">
            // 图片页面
            // list-type 文件列表的类型text/picture/picture-card
            // auto-upload   是否在选取文件后立即进行上传
          <el-upload
            action="#"
            list-type="picture-card"
            :auto-upload="false"
          >
            <i slot="default" class="el-icon-plus" />
            <div slot="file" slot-scope="{file}">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              >
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in" />
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <i class="el-icon-download" />
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete" />
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item> -->
      </el-form>
      <div>
        <el-button @click="cancelMenu">取消</el-button>
        <el-button type="primary" @click="createMenu(menuForm)">确定</el-button>
      </div>
    </el-dialog>

    <!-- 修改按钮页面 -->
    <el-dialog
      :visible.sync="editVisible"
      width="30%"
      :before-close="
        () => {
          editVisible = false
        }
      "
      :show-close="false"
    >
      <el-form
        ref="editmenuForm"
        :model="editmenuForm"
        :rules="rules"
        :label-position="labelPosition"
        label-width="10vw"
      >
        <el-form-item label="维修人员产品描述" prop="worlDescribe">
          <el-input v-model="editmenuForm.worlDescribe" />
        </el-form-item>
        <el-form-item label="目前维修进度" prop="nowState">
          <el-input v-model="editmenuForm.nowState" />
        </el-form-item>
        <el-form-item label="维修状态" prop="maintainState">
          <el-radio-group v-model="editmenuForm.maintainState">
            <el-radio :label="0">未完成</el-radio>
            <el-radio :label="1">完成</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item
          v-if="editmenuForm.courseisfree == 0"
          label="原价"
          prop="Original"
        >
          <el-input v-model="editmenuForm.Original" />
        </el-form-item>
        <el-form-item
          v-if="editmenuForm.courseisfree == 0"
          label="优惠价"
          prop="Discount"
        >
          <el-input v-model="editmenuForm.Discount" />
        </el-form-item>
        <el-form-item label="可选题库" prop="coursetiku">
          <el-input v-model="editmenuForm.coursetiku" />
        </el-form-item>
        <el-form-item label="课程简介" prop="courseintroduction">
          <el-input v-model="editmenuForm.courseintroduction" />
        </el-form-item> -->
        <!-- <el-form-item label="课程封面" prop="coursecover"> -->
        <!--
            图片页面
            list-type 文件列表的类型text/picture/picture-card
            auto-upload   是否在选取文件后立即进行上传
           -->
        <!-- <el-upload
            action="#"
            list-type="picture-card"
            :auto-upload="false"
          >
            <i slot="default" class="el-icon-plus" />
            <div slot="file" slot-scope="{file}">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              >
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-preview"
                  @click="handlePictureCardPreview(file)"
                >
                  <i class="el-icon-zoom-in" />
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleDownload(file)"
                >
                  <i class="el-icon-download" />
                </span>
                <span
                  v-if="!disabled"
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete" />
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item> -->
      </el-form>
      <div>
        <el-button @click="editcancelMenu">取消</el-button>
        <el-button type="primary" @click="editMenu(editmenuForm)">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postList, postUpData, delect, conditionsfind } from '@/api/table'
import { getToken } from '@/utils/auth'
// , setToken, removeToken, setRole, removeRole
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      select2: '',
      select3: '',
      createVisible: false,
      editVisible: false,
      labelPosition: 'left',
      menuForm: { // 添加部分
        customerID: 0,
        productID: '',
        customerDescribe: ''
      },
      editmenuForm: { // 修改部分
        id: 0,
        maintainState: [0, 1], // 维修状态
        worlDescribe: '', // 测试人员描述
        nowState: '' // 目前维修进度
      },
      form: {
        delivery: false
      },
      dialogImageUrl: '', // 图片提交部分 对话框图片网址:
      dialogVisible: false, // 图片提交部分 对话框可见:假的,
      disabled: false, // 图片提交部分 禁用:假
      rules: { // 验证规则配置
        customerDescribe: [
          {
            required: true,
            message: '请输入故障描述',
            trigger: 'blur'
          }
        ],
        productID: [
          {
            required: true,
            message: '请输入产品序列号',
            trigger: 'blur'
          }
        ],
        worlDescribe: [
          {
            required: true,
            message: '请输入描述',
            trigger: 'blur'
          }
        ],
        maintainState: [
          {
            required: true,
            message: '维修状态不能为空',
            trigger: 'blur'
          }
        ],
        nowState: [
          {
            required: true,
            message: '目前维修状态不能为空',
            trigger: 'blur'
          }
        ]
      }, // 验证规则配置
      map: {
        productID: '',
        maintainState: [0, 1]
      } //  表单提交
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    shujuqxi(response) { // 数据清洗
      var a = response.data
      for (let I = 0; I < a.length; I++) {
        if (a[I].MaintainState) {
          a[I].MaintainState = '通过'
        } else {
          a[I].MaintainState = '不通过'
        }
      }
      if (a != null) {
        this.list = a
      }
    },
    fetchData() {
      this.listLoading = true
      postList({ customerID: getToken().substring(1), nowState: getToken().substring(0, 1) }).then((response) => { // 课程表获取数据
        this.shujuqxi(response)
        this.listLoading = false
      })
    },
    reset() {
      this.map.productID = ''
      this.map.maintainState = ''
    },
    handlecreate() {
      if (getToken()[0] === 'C') {
        // console.log(getToken().substring(1))
        this.menuForm.customerID = getToken().substring(1)
        this.createVisible = true
      } else {
        // console.log('没有操作权限')
        alert('没有操作权限')
      }
    },
    handleEdit(id) {
      if (getToken()[0] === 'T') {
        this.editmenuForm.id = id
        this.editVisible = true
      } else {
        // console.log('没有操作权限')
        alert('没有操作权限')
      }
      // this.$refs.update.open();
    },
    cancelMenu() {
      this.createVisible = false
      // console.log(this.menuForm)
      this.createVisible = false
    },
    Coursedelete(id) { // 删除按钮
      // console.log(typeof id)
      if (getToken()[0] === 'T') {
        if (id != null) {
          delect(id)
          this.fetchData()
        } else {
          console.log('error delete')
        }
      } else {
        console.log('没有操作权限')
        alert('没有操作权限')
      }
    },
    createMenu(data) { // 添加按钮
      this.$refs['menuForm'].validate((valid) => {
        if (valid) {
          postUpData(data).then((response) => {
            // console.log(response)
            this.fetchData()
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          })
        } else {
          console.log('error submit!')
          return false
        }
      })
      this.createVisible = false
    },
    editMenu(data) { // 修改按钮
      this.$refs['editmenuForm'].validate((valid) => {
        if (valid) {
          postUpData({ id: data.id, worlDescribe: data.worlDescribe, nowState: data.nowState, maintainState: data.maintainState }).then((response) => {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          })
        } else {
          console.log('error submit!')
          return false
        }
      })
      this.editVisible = false
    },
    editcancelMenu() {
      this.editVisible = false
    },
    // 根据条件查询
    selectdata() {
      this.$refs['ruleForm'].validate((valid) => {
        conditionsfind({ customerID: parseInt(getToken().substring(1)), productID: this.map.productID, maintainState: parseInt(this.map.maintainState) }).then((response) => {
          this.list = response.data
          this.$message({
            type: 'success',
            message: '操作成功'
          })
        })
      })
    }
  }
}
</script>
